/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;

import algo.Database;
import algo.Simplification;

/**
 *
 * @author ceikute
 */
public class HandleFolder {

	
    public String removeWhiteSpacesInFiles(String folderUrl) throws Exception {
        String resUrl = folderUrl + "\\result";
        File resFolder = new File(resUrl);
        resFolder.mkdir();

        File inFolder = new File(folderUrl);
        File[] listOfFiles = inFolder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
//        for (int i = 4; i < 5; i++) {
            if (listOfFiles[i].isFile()) {
                System.out.println("File " + listOfFiles[i].getName());
                prehandleFiles(folderUrl, resUrl, listOfFiles[i].getName());
//                getStartLine(folderUrl + "\\result\\" + listOfFiles[i].getName());

            } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());
            }
        }
        return resUrl;
    }

    public void prehandleFiles(String fileUrl, String resultUrl, String fName) throws Exception {
        try {
            FileInputStream fstream = new FileInputStream(fileUrl + "\\" + fName);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String[] fileNameArr = fName.split(".txt");
            String fileName = resultUrl + "\\" + fileNameArr[0] + "_1.txt";

            FileWriter fstreamOut = new FileWriter(fileName);
            BufferedWriter out = new BufferedWriter(fstreamOut);

            String strLine;

            while ((strLine = br.readLine()) != null) {
                String[] arr = strLine.trim().split(" ");
                String newLine = "";
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i].length() > 0) {
                        newLine = newLine + arr[i] + " ";
                    }
                }
                if (newLine.length() > 0) {
                    out.write(newLine.trim() + "\n");
                    out.flush();
                }
            }
            out.flush();
            in.close();
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    public String generateSimplifiedVersion(String folderUrl) throws Exception {
        System.out.println("folder url " + folderUrl);
        String resUrl = folderUrl + "\\simplified";
        File resFolder = new File(resUrl);
        resFolder.mkdir();

        File inFolder = new File(folderUrl);
        File[] listOfFiles = inFolder.listFiles();
        System.out.println("files " + listOfFiles.length);
//        for (int i = 0; i < listOfFiles.length; i++) {
        for (int i = 1; i < 2; i++) {
            if (listOfFiles[i].isFile()) {
                System.out.println("File " + listOfFiles[i].getName());
                Simplification simpl = new Simplification();

                Integer[] resArr = simpl.getStartRoute(folderUrl + "\\" + listOfFiles[i].getName(), 0, 100.0);
                if (resArr != null) {
                    System.out.println(resArr.length);
                    createSimplifiedFile(folderUrl, listOfFiles[i].getName(), resArr);
                }

            } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());
            }
        }
        return resUrl;
    }

    /**
     * 
     * @param folderUrl
     * @param fileNameStart
     * @param linesArr
     */
    private void createSimplifiedFile(String folderUrl, String fileNameStart, Integer[] linesArr) {
        String fileUrl = folderUrl + "\\" + fileNameStart;
        try {
            FileInputStream fstream = new FileInputStream(fileUrl);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String strLine = null;
            ArrayList<String> fileLines = new ArrayList();
            while ((strLine = br.readLine()) != null) {
                fileLines.add(strLine);
            }
            in.close();
            System.out.println(fileLines.size());
            String[] lines = new String[fileLines.size()];

            fileLines.toArray(lines);

            String[] fileNameArr = fileNameStart.split(".txt");
            String fileName = folderUrl + "\\simplified\\" + fileNameArr[0] + "_simpl.txt";

            FileWriter fstreamOut = new FileWriter(fileName);
            BufferedWriter out = new BufferedWriter(fstreamOut);

            out.write(lines[0] + "\n");
            for (int i = 0; i < linesArr.length; i++) {
                out.write(lines[linesArr[i]] + "\n");
                out.flush();
            }
            out.write(lines[lines.length] + "\n");
            out.flush();
            out.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
    }

    public void imoprtDataToDatabase(String folderUrl) throws SQLException {
        File inFolder = new File(folderUrl);
        File[] listOfFiles = inFolder.listFiles();
        System.out.println(folderUrl);
        Database db = null;

        try {
            db = new Database();

//        for (int i = 0; i < listOfFiles.length; i++) {
            for (int i = 0; i < 1; i++) {
                if (listOfFiles[i].isFile()) {
                    System.out.println("File " + listOfFiles[i].getName());
                    String fName = listOfFiles[i].getName();

                    FileInputStream fstream = new FileInputStream(folderUrl + "\\" + fName);
                    DataInputStream in = new DataInputStream(fstream);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));

                    String strLine;
                    strLine = br.readLine();
                    System.out.println(strLine);
                    while ((strLine = br.readLine()) != null) {
                        System.out.println(strLine);
                        db.insertData(strLine);
                    }

                }
            }
        } catch (Exception ex) {
            db.closeConnection();
        }
    }

    /**
     * Connecting to postgresql database
     * @param carId
     * @param driverId
     * @throws SQLException
     * @throws IOException
     */
    public void getRoutes(String carId, String driverId) throws SQLException, IOException {
        Database db = new Database();
        db.generateRouteFiles(carId, driverId);
        db.closeConnection();
    }
    
   
}
